package leetcode;

import java.util.Arrays;

public class Leetcode654 {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        if(nums.length==0){
            return null;
        }
        int max = nums[0];
        int index = 0;
        for (int i = 1; i < nums.length; i++) {
            if(nums[i]>max){
                index = i;
                max = nums[i];
            }
        }
        TreeNode root = new TreeNode(max);
        root.left = constructMaximumBinaryTree(Arrays.copyOfRange(nums,0,index));
        root.right = constructMaximumBinaryTree(Arrays.copyOfRange(nums,index+1,nums.length));
        return root;
    }
}
